package com.hzy.chinese.jchess.utils;

import android.annotation.SuppressLint;

import com.avos.avoscloud.AVACL;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.SignUpCallback;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * author : YYCHEN
 * e-mail : xxx@xx
 * time   : 2018/06/08
 * desc   :
 * version: 1.0
 */
public final class DbUtil {


    @SuppressLint("CheckResult")
    public static void registerUser(final String username, final String email, final String password, final SignUpCallback callback){

        Observable.create(new ObservableOnSubscribe<AVUser>() {
            @Override
            public void subscribe(ObservableEmitter<AVUser> emitter) throws Exception {
                //注册该账号
                AVUser user = new AVUser();
                user.setUsername(username);
                user.setPassword(password);
                user.setEmail(email);
                user.signUp();
                emitter.onNext(user);
            }
        }).subscribeOn(Schedulers.io())
                .map(new Function<AVUser, AVUser>() {
                    @Override
                    public AVUser apply(AVUser user) throws Exception {
                        //添加账号数据权限
                        AVACL acl = new AVACL();
                        acl.setPublicReadAccess(true);
                        acl.setWriteAccess(user, true);
                        user.setACL(acl);
                        user.save();
                        return user;
                    }
                }).map(new Function<AVUser, AVUser>() {
                    @Override
                    public AVUser apply(AVUser user) throws Exception {
                        //初始化个人信息
                        initUserPersonInfo(user);
                        return user;
                    }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<AVUser>() {
                    @Override
                    public void accept(AVUser user) throws Exception {
                        callback.done(null);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        callback.done((AVException) throwable);
                    }
                });

    }

    private static AVObject initUserPersonInfo(AVUser user) throws AVException {
        AVObject object = new AVObject(PERSON_INFO.TABLE_NAME);
        object.put(PERSON_INFO.KEY_SCORE, 1000);  //初始战力均为1000
        object.put(PERSON_INFO.KEY_USER_ID, user);
        AVACL acl = new AVACL();
        acl.setPublicReadAccess(true);
        acl.setWriteAccess(user, true);
        object.setACL(acl);
        object.save();
        return object;
    }

    public static void loginUser(String username, String password, LogInCallback callback){
        AVUser.logInInBackground(username, password, callback);
    }



    public static final class PERSON_INFO{
        public static final String TABLE_NAME = "PersonInfo";
        public static final String KEY_SCORE = "score";
        public static final  String KEY_USER_ID = "userid";

    }

}
